package book;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockAwaitNotifyTest implements Runnable{

	private Lock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();

	@Override
	public void run() {

		lock.lock();
		System.out.println("进入锁");
		try {
			System.out.println("进入等待");
			condition.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			System.out.println("离开锁");
			lock.unlock();
		}
	}
	
	public void signal(){
		
		lock.lock();
		try {
			System.out.println("唤醒");
			condition.signal();
		} finally {
			lock.unlock();
		}
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		ReentrantLockAwaitNotifyTest lockTask = new ReentrantLockAwaitNotifyTest();
		Thread t = new Thread(lockTask);
		
		t.start();
		
		Thread.sleep(1000);
		lockTask.signal();
	}
}
